package com.hainatong.shiphelper.module.holiday.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentHolidayApplyDetailBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.mainhome.adapter.ExamineStepAdapter;
import com.hainatong.shiphelper.mainhome.adapter.TimeLineAdapter;
import com.hainatong.shiphelper.mainhome.entity.ExamineStepBean;
import com.hainatong.shiphelper.module.holiday.entity.AuditProcessBean;
import com.hainatong.shiphelper.module.holiday.entity.UserVacationBean;
import com.hainatong.shiphelper.module.holiday.viewmodel.VacationViewModel;
import com.hainatong.shiphelper.utils.GsonUtil;


import java.util.ArrayList;
import java.util.List;

public class HolidayApplyResultFragment extends MvvmLazyFragment<FragmentHolidayApplyDetailBinding, VacationViewModel>
{
    private static String APPLY_RESULT_TAG = "HolidayApplyResultFragment";


    private UserVacationBean vacationBean;

    private ExamineStepAdapter adapter;

    private List<ExamineStepBean> stepList;

    private List<AuditProcessBean>  auditProcessList;

    public static HolidayApplyResultFragment newInstance(String typeCode)
    {
        Bundle bundle = new Bundle();
        bundle.putString( APPLY_RESULT_TAG , typeCode );
        HolidayApplyResultFragment applyFragment = new HolidayApplyResultFragment();
        applyFragment.setArguments( bundle );
        return applyFragment;
    }

    @Override
    protected void initParameters()
    {
        Bundle args = getArguments();
        String type = "";
        if(  args != null )
        {
            type = args.getString(APPLY_RESULT_TAG , "");
        }

        if( !StringUtils.isEmpty( type ))
        {
            vacationBean = GsonUtil.json2Obj( type,UserVacationBean.class );
        }
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_holiday_apply_detail;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    @Override
    protected void initView(View view)
    {
        viewDataBinding.rvHolidayTime.setLayoutManager(new LinearLayoutManager(getActivity()));
        viewDataBinding.rvHolidayTime.hasFixedSize();

        adapter = new ExamineStepAdapter();
        stepList = new ArrayList<>();

        viewDataBinding.rvProcessStep.setLayoutManager(new LinearLayoutManager(getActivity()));
        viewDataBinding.rvProcessStep.hasFixedSize();
        viewDataBinding.rvProcessStep.setAdapter( adapter );
        /*
        viewDataBinding.holidayStepView.setStepViewTexts(timelist)
                                       .setStepsViewIndicatorComplectingPosition(timelist.size())
                                       .setLinePaddingProportion(0.85f)
                                       .setTextSize(12)
                                       .setStepViewComplectedTextColor(ContextCompat.getColor(getActivity(), R.color.gray_point))
                                       .setStepViewUnComplectedTextColor(ContextCompat.getColor(getActivity(), R.color.gray_point))
                                       .setStepsViewIndicatorCompletedLineColor(ContextCompat.getColor(getActivity(), R.color.gray_point))
                                       .setStepsViewIndicatorUnCompletedLineColor(ContextCompat.getColor(getActivity(), R.color.gray_point))
                                       .setStepsViewIndicatorDefaultIcon(ContextCompat.getDrawable(getActivity(),R.drawable.shape_oval_gray_point))
                                       .setStepsViewIndicatorAttentionIcon(ContextCompat.getDrawable(getActivity(),R.drawable.shape_oval_gray_point))
                                       .setStepsViewIndicatorCompleteIcon(ContextCompat.getDrawable(getActivity(),R.drawable.shape_oval_gray_point));
        */


        viewDataBinding.rlApplyHistory.setOnClickListener( v -> {
            //
            ComFragmentActivity.startViewByParam( getActivity(), Constants.VIEW_USER_VACATION_HISTORY , vacationBean.getUser_id() );
        } );



    }

    @Override
    protected void lazyLoad()
    {
        if(ObjectUtils.isNotEmpty( vacationBean ))
        {
            TimeLineAdapter lineAdapter = TimeLineAdapter.newInstance( new String[]{vacationBean.getStartdate(),vacationBean.getEnddate()} );
            viewDataBinding.rvHolidayTime.setAdapter( lineAdapter );


            viewDataBinding.tvVacationTotalTime.setText( "休假"+vacationBean.getTotal()+"小时" );

            //查询 改 休假的 审核流程  vacationBean.getUuid()
            viewModel.getAuditProcessByVacId( "6e9d06ed-ba76-4b83-9ed0-c6511c7d8379" );


            ExamineStepBean step = new ExamineStepBean();
            step.setStatus( ExamineStepBean.StepStatus.START );
            step.setActiveTime( vacationBean.getCreateDate() );
            step.setStepName("发起申请");
            step.setSubName(vacationBean.getUsername());
            stepList.add( step );

            // 1 已申请  2 审核中 3 通过  4 驳回
            int status = Integer.parseInt( vacationBean.getStatus() );
            switch ( status )
            {
                case 1:
                case 2:

                    viewDataBinding.ivApplyResult.setImageResource(R.mipmap.ic_toreview_zh);
                    // 根据中英文 切换
                    //viewDataBinding.ivApplyResult.setImageResource(R.mipmap.ic_toreview_en);

                    break;
                case 3:
                    viewDataBinding.ivApplyResult.setImageResource(R.mipmap.ic_passed_zh);
                    // 根据中英文 切换
                    //viewDataBinding.ivApplyResult.setImageResource(R.mipmap.ic_passed_en);
                    break;
                case 4:
                    viewDataBinding.ivApplyResult.setImageResource(R.mipmap.ic_reject_zh);
                    // 根据中英文 切换
                    //viewDataBinding.ivApplyResult.setImageResource(R.mipmap.ic_reject_en);
                    break;
            }

        }

        // 审批 流程
        viewModel.getAuditList().observe( this ,result -> {
            if( result.isSuccess() )
            {
                if( result.getData() != null )
                {
                    auditProcessList = result.getData();
                    for(int i=0; i < auditProcessList.size() ; i++ )
                    {
                        AuditProcessBean auditTemp = new AuditProcessBean();
                        if( i ==  auditProcessList.size()-1)
                        {
                            ExamineStepBean step3 = new ExamineStepBean();
                            step3.setStatus( ExamineStepBean.StepStatus.END );
                            step3.setActiveTime("时间:"+ auditTemp.getAudit_time() );
                            step3.setStepName("user:"+ auditTemp.getAudit_name() );
                            if( auditTemp.getAudit_status() == "1" )
                            {
                                step3.setSubName("（已同意）");
                            }
                            else
                            {
                                step3.setSubName("（待审核）");
                            }
                            stepList.add( step3 );
                        }
                        else
                        {
                            ExamineStepBean step2 = new ExamineStepBean();
                            step2.setStatus( ExamineStepBean.StepStatus.ACTIVE );
                            step2.setActiveTime( "时间:"+auditTemp.getAudit_time() );
                            step2.setStepName("user:"+auditTemp.getAudit_name() );
                            if( auditTemp.getAudit_status() == "1" )
                            {
                                step2.setSubName("（已同意）");
                            }
                            else
                            {
                                step2.setSubName("（待审核）");
                            }
                            stepList.add( step2 );
                        }
                    }
                }
                adapter.setList( stepList );
            }
            else
            {

            }
        } );

    }
}
